package com.lb.service.building;

import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lb.entity.building.Building;
import com.lb.entity.building.BuildingExample;
import com.lb.mapper.building.BuildingMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.spatial4j.io.GeohashUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;

import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class BuildingService extends ServiceImpl<BuildingMapper, Building> {

    private final BuildingMapper buildingMapper;


    public long countByExample(BuildingExample example) {
        return baseMapper.countByExample(example);
    }

    public int deleteByExample(BuildingExample example) {
        return baseMapper.deleteByExample(example);
    }

    public List<Building> selectByExample(BuildingExample example) {
        return baseMapper.selectByExample(example);
    }

    public int updateByExampleSelective(Building record, BuildingExample example) {
        return baseMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Building record, BuildingExample example) {
        return baseMapper.updateByExample(record, example);
    }

    public void initializeBuildingGEO() {

        // 总数据
        long sum = this.baseMapper.countByExample(new BuildingExample());
        log.info("总条数:{}", sum);
        // 一次处理一万条数据
        int pageNum = (int) Math.ceil((double) sum / 1000);
        for (int i = 1; i <= pageNum; i++) {
            Page<Building> buildingPage = buildingMapper.selectPage(new Page<>(i, 1000), new QueryWrapper<>());
            List<Building> records = buildingPage.getRecords();
            BuildingService service = SpringUtil.getBean(BuildingService.class);
            service.handleNoGEO(records);
        }
    }

    /**
     * 异步处理数据
     *
     * @param records 集合数据
     * @author libin
     * @date 2023/7/14 16:59
     **/
    @Async("taskExecutor")
    @Transactional(rollbackFor = Exception.class)
    public void handleNoGEO(List<Building> records) {

        for (Building building : records) {
            if (StringUtils.isNotBlank(building.getLongitude()) && StringUtils.isNotBlank(building.getLatitude())) {
                // 计算geoCode
                String geoCode = GeohashUtils.encodeLatLon(Double.parseDouble(building.getLatitude()),
                        Double.parseDouble(building.getLongitude()));
                building.setGeoCode(geoCode);
            }
        }
        this.updateBatchById(records);
    }

    public void test1() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try {
            String body = HttpUtil.createGet("localhost:9001/v1/api/third/test2")
                    .timeout(3000)
                    .execute().body();
            log.info(body);
        } catch (HttpException e) {
            e.printStackTrace();
        }finally {
            stopWatch.stop();
            log.info("时间是:{}",stopWatch.getLastTaskTimeMillis());
        }
    }

    public void test2() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
